import 'dart:convert';
import 'dart:io';

import 'package:capsule/models/config_file_type.dart';

class Config {
  List<ConfigFileType>? _fileTypes;
  List<ConfigFileType>? get fileTypes => _fileTypes;
  Future<void> readFromFile() async {
    String appPath = Directory.current.path;
    String configPath = "$appPath\\config.json";
    File configFile = File(configPath);
    bool exists = await configFile.exists();
    if (exists == true) {
      String jsonString = await configFile.readAsString();
      final config = json.decode(jsonString);
      _parseFileTypes(config);
    }
  }

  void _parseFileTypes(Map<String, dynamic> config) {
    final fileTypesProp = config["fileTypes"];
    if (fileTypesProp != null) {
      _fileTypes = [];
      for (final ft in fileTypesProp) {
        _fileTypes!.add(ConfigFileType.parse(ft));
      }
    }
  }
}
